<?php
namespace Home\Controller;
use Think\Controller;
class UcController extends CommonController {
    public function index() {
        $this->display('PersonalCenter/index');
    }
    public function GetAllBet() {
        $list = M('sj')->field('hongtl,DFW')->where('Id=1')->find();
        $date['success'] = true;
        $date['list'] = $list;
        $this->ajaxReturn($date);
    }
    public function SubmitBet() {
        $data['success'] = false;
        $hongtl = (int)trim(I('post.hongtl'));
        $DFW = (int)trim(I('post.DFW'));
        if ($hongtl > 0) {
            if ($hongtl < 100 || $hongtl > 10000000 || $hongtl % 100 > 0) {
                $data['msg'] = '投注金额不符合规则';
            }
        }
        if ($DFW > 0) {
            if ($DFW < 100 || $DFW > 10000000 || $DFW % 100 > 0) {
                $data['msg'] = '投注金额不符合规则';
            }
        }
        $sum = $hongtl + $DFW;
        if ($data['msg']) {
            $this->ajaxReturn($data);
        }
        $list = M('user')->where(array(
            'Id' => $_SESSION['uid']
        ))->find();
        if ($sum > $list['monery']) {
            $data['msg'] = '账户余额不足';
            $this->ajaxReturn($data);
	    }
        list($s1, $s2) = explode(' ', microtime());
        $s3 = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        $IssueNum = (date('Ymd', $s2) * 10000) + (ceil(($s2 - strtotime(date('Y-m-d', $s2))) / 60));
        $StartTime = strtotime(date('Y-m-d H:i', $s2));
        $S4 = ($StartTime + 30) * 1000;
        $nTime = $S4 - $s3;
        if ($nTime < 0) {
            $data['msg'] = '第' . $IssueNum . '期已停止投注！';
            $this->ajaxReturn($data);
        }
       M('user')->startTrans();
        $sorder = M('bet')->where(array('IssueNum' => $IssueNum,'UserID' => $_SESSION['uid']))->field('Id')->find();
        if (!$sorder) {
            $data2 = array();
            $data2['IssueNum'] = $IssueNum;
            $data2['zMoney'] = $hongtl;
            $data2['fMoney'] = $DFW;
            $data2['CreateTime'] = $s2;
            $data2['UserID'] = $_SESSION['uid'];
            $result = M('bet')->data($data2)->add();
        } else {
            $data2 = array(
                'zMoney' => array('exp', '`zMoney`+' . $hongtl),
                'fMoney' => array('exp', '`fMoney`+' . $DFW) ,
            );
            $result = M('bet')->where(array('IssueNum' => $IssueNum,'UserID' => $_SESSION['uid']))->data($data2)->save();
        }
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}
        $result = M('user')->where(array('Id' => $_SESSION['uid']))->setDec('monery', $sum);
	if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}
        $result = M('user')->where(array('Id' => $_SESSION['uid']))->setInc('betcountmoney', $sum);
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}
        if (C('WRITE_BUG')) {
        $data2 = array();
        $data2['type'] = 2;
        $data2['amount'] = $sum;
        $data2['info'] = $IssueNum . '(投注)';
        $data2['time'] = time();
        $data2['uid'] = $_SESSION['uid'];
        $result = M('monery')->data($data2)->add();
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}}           
        for ($i = 1; $i < 2; $i++) {
    	$rmoney = $sum * C('BAT_ONE');
        $list = M('user')->where(array('Id' => $list['tid']))->find();
        if ($list['id'] > 1) {
        $data2 = array('ratiomoneyall' => array('exp', '`ratiomoneyall`+' . $rmoney) ,'rmonery' => array('exp', '`rmonery' . '`+' . $rmoney) ,);
        $result = M('user')->where(array('Id' => $list['id']))->data($data2)->save();
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!' . $i;$this->ajaxReturn($data);}
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}
        if (C('WRITE_BUG')) {$data2 = array();$data2['type'] = 1;
        $data2['amount'] = $rmoney;
        $data2['info'] = $IssueNum . '提成' . $i;
        $data2['time'] = time();
        $data2['uid'] = $list['id'];
        $result = M('monery')->data($data2)->add();
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}}
        $rmoney = $rmoney / 2;} else {break;}
		if(C('BAT_TWO_ON') && $list2 = M('user')->where(array('Id' => $list['tid']))->find()){
        for ($i = 1; $i < 2; $i++) {
    	$rmoney = $sum * C('BAT_TWO');
        if ($list2['id'] > 1) {
        $data2 = array('ratiomoneyall' => array('exp', '`ratiomoneyall`+' . $rmoney) ,'rmonery2' => array('exp', '`rmonery2' . '`+' . $rmoney) ,);
        $result = M('user')->where(array('Id' => $list2['id']))->data($data2)->save();
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!' . $i;$this->ajaxReturn($data);}
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}
        if (C('WRITE_BUG')) {//加入日志
        $data2 = array();
        $data2['type'] = 1;
        $data2['amount'] = $rmoney;
        $data2['info'] = $IssueNum . '2级提成' . $i;
        $data2['time'] = time();
        $data2['uid'] = $list2['id'];
        $result = M('monery')->data($data2)->add();
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}}
        $rmoney = $rmoney / 2;} else {break;}}}
		$payall=M('user')->field('payall')->where(array('Id' => $_SESSION ['uid']))->find();
		if(C('BAT_SALF_ON') && $payall>=C('BAT_SALF_LV')){
		$list2 = M('user')->where(array('Id' => $_SESSION['uid']))->find();
        for ($i = 1; $i < 2; $i++) {
    	$rmoney = $sum * C('BAT_SALF');
        if ($list2['id'] > 1) {
        $data2 = array('fmoneryall' => array('exp', '`fmoneryall`+' . $rmoney) ,'fmonery' => array('exp', '`fmonery' . '`+' . $rmoney) ,);
        $result = M('user')->where(array('Id' => $list2['id']))->data($data2)->save();
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!' . $i;$this->ajaxReturn($data);}
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}
        if (C('WRITE_BUG')) {
        $data2 = array();
        $data2['type'] = 1;
        $data2['amount'] = $rmoney;
        $data2['info'] = $IssueNum . '个人返水' . $i;
        $data2['time'] = time();
        $data2['uid'] = $list2['id'];
        $result = M('monery')->data($data2)->add();
        if (!$result) {M('user')->rollback();$data['msg'] = '投注失败!';$this->ajaxReturn($data);}}} else {break;}}}}
        M('user')->commit();
        $data['msg'] = '投注成功！';
        $data['success'] = true;
        $map = array();
        $map['IssueNum'] = array('EQ',$IssueNum);
        $map['UserID'] = array('EQ',$_SESSION['uid']);
        $data2['zBet'] = M('bet')->where($map)->sum('zMoney');
        $map = array();
        $map['IssueNum'] = array('EQ',$IssueNum);
        $map['UserID'] = array('EQ',$_SESSION['uid']);
        $data2['fBet'] = M('bet')->where($map)->sum('fMoney');
        $data['list'] = $data2;
        unset($data2, $list, $map);
        $this->ajaxReturn($data);
    }
    public function GetUserInfo() {
        if (!IS_POST || !'IS_AJAX') {
            $this->redirect('/');
        }
        $time = time();
        $list = M('user')->field('monery')->where(array('Id' => $_SESSION['uid']))->find();
        $data['CreateTime'] = array('between',array($_SESSION['logintime2'],$time));
        $data['UserID'] = array('EQ',$_SESSION['uid']);
        $_SESSION['sumMoney'] = M('bet')->where($data)->sum('winMoney');
        $_SESSION['UserMoney'] = $list['monery'];
        $arr['UserName'] = $_SESSION['username'];
        $arr['UserMoney'] = $_SESSION['UserMoney'];
        $arr['sumMoney'] = $_SESSION['sumMoney'];
        $data = array();
        $data['success'] = true;
        $data['list'] = $arr;
        unset($arr, $list);
        $this->ajaxReturn($data);
	}
    public function GetKJIssue() {
        list($s1, $s2) = explode(' ', microtime());
        $s3 = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        $StartTime = strtotime(date('Y-m-d H:i', $s2));
        $s4 = ($StartTime + 55);
        $IssueNum = (date('Ymd', $s2) * 10000) + ceil(($s2 - strtotime(date('Y-m-d', $s2))) / 60);
        if ($s2 < $s4) {
            $IssueNum = $IssueNum - 1;
            $nTime = $s4 * 1000 - $s3;
        } else {
            $nTime = ($s4 + 60) * 1000 - $s3;
        }
        $list = M('lotteryrecord')->where(array('IssueNum' => $IssueNum))->find();
        if ($list) {
            $data2['IssueNum'] = $list['issuenum'];
            $data2['LotteryNum'] = $list['lotterynum'];
            $data2['StartTime'] = $list['starttime'];
            $data2['EndTime'] = $list['endtime'];
            $data2['Status'] = $list['status'];
            $data2['KJhm'] = $list['kjhm'];
            $data['list'] = $data2;
            $data['success'] = true;
            $data['nTime'] = $nTime;
            $this->ajaxReturn($data);
        }
    }
    public function GetLastNum() {
        if (!IS_POST || !'IS_AJAX') {
            $this->redirect('/');
        }
        $data['success'] = true;
        $data['list'] = M('lotteryrecord')->where(array(
            'Status' => 1
        ))->order('Id desc')->limit(7)->select();
        $this->ajaxReturn($data);
    }
    public function GetIssue() {
        if (!IS_POST || !'IS_AJAX') {
            $this->redirect('/');
        }
        $data['success'] = true;
        list($s1, $s2) = explode(' ', microtime());
        $s3 = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        $StartTime = strtotime(date('Y-m-d H:i', $s2));
        $S4 = ($StartTime + 30) * 1000;
        $nTime = $S4 - $s3;
        $IssueNum = (date('Ymd', $s2) * 10000) + (ceil(($s2 - strtotime(date('Y-m-d', $s2))) / 60));
        if ($nTime < 0) {
            $StartTime = strtotime(date('Y-m-d H:i', $s2 + 60));
            $S4 = $S4 + 60000;
            $nTime = $S4 - $s3;
            $IssueNum = $IssueNum + 1;
        }
        $EndTime = $StartTime + 60;
        $data2['IssueNum'] = $IssueNum;
        $data2['StartTime'] = $StartTime;
        $data2['EndTime'] = $EndTime;
        $data['list'] = $data2;
        $data['nTime'] = $nTime;
        unset($data2);
        $this->ajaxReturn($data);
    }
}